package com.example.administrator.androiddate;

import android.content.Context;
import android.media.AudioManager;
import android.media.SoundPool;
import android.util.Log;

/**
 * Created by Administrator on 2017\12\20 0020.
 */

public class SoundPlayUtils {
    private Context context;
    private SoundPool soundPool;
    private int soundID;

    public SoundPlayUtils(Context context) {
        this.context = context;
        this.soundPool= new SoundPool(3, AudioManager.STREAM_SYSTEM, 5);
    }

    /**
     * 加载声音资源
     *
     *
     * @return soundID
     */
    public int loadRes(int resId) {
        return this.soundPool.load(this.context,resId, 1);
    }

    /**
     * 播放声音
     *
     * @param soundID
     * @param priority
     * @param isLoop
     */
    public void play(int soundID, int priority, boolean isLoop) {
        this.soundPool.play(soundID, 1,1,priority, isLoop ? -1 : 0, 1);
    }

     /**
     * 停止
     *
     * @param soundID
     */
    public void stop(int soundID) {
        this.soundPool.stop(soundID);
        this.soundPool.release();
    }

//    /**
//     * 播放声音  修改后的
//     *
//     * @param resId
//     * @param isLoop
//     */
    public void play(int resId, final boolean isLoop) {
        this.soundID = resId;
        this.soundPool.setOnLoadCompleteListener(new SoundPool.OnLoadCompleteListener() {
            @Override
            public void onLoadComplete(SoundPool soundPool, int sampleId, int status) {
                Log.d("------","来了");

                play(soundID, 1, isLoop);

            }
        });
    }
    /**
     * 播放声音
     *
     * @param resId
     * @param isLoop
     */
//    public void play(int resId, final boolean isLoop) {
//        this.soundID = loadRes(resId);
//        this.soundPool.setOnLoadCompleteListener(new SoundPool.OnLoadCompleteListener() {
//            @Override
//            public void onLoadComplete(SoundPool soundPool, int sampleId, int status) {
//                play(soundID, 1, isLoop);
//
//            }
//        });
//    }
    /**
     * 停止
     */
    public void stop() {
        stop(this.soundID);
    }
}
